<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <!-- 引入组件库 -->
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script src="../js/vue.js"></script>
        <script src="../plugins/elementui/index.js"></script>
        <script src="../js/axios-0.18.0.js"></script>
        <script type="text/javascript" src="https://api.map.baidu.com/getscript?v=2.0&ak=0dGiOpogZZhb28RSrk0Pu5vfVYPFnhof"></script>
        <style>
            .avatar-uploader .el-upload {
                border: 1px dashed #d9d9d9;
                border-radius: 6px;
                cursor: pointer;
                position: relative;
                overflow: hidden;
            }

            .avatar-uploader .el-upload:hover {
                border-color: #409EFF;
            }

            .avatar-uploader-icon {
                font-size: 28px;
                color: #8c939d;
                width: 178px;
                height: 178px;
                line-height: 178px;
                text-align: center;
            }

            .avatar {
                width: 178px;
                height: 178px;
                display: block;
            }

            .datatable {
                position: relative;
                box-sizing: border-box;
                -webkit-box-flex: 1;
                width: 100%;
                max-width: 100%;
                font-size: 14px;
                color: rgb(96, 98, 102);
                overflow: hidden;
                flex: 1 1 0%;
            }

            .datatable td, .datatable th {
                padding: 12px 0;
                min-width: 0;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                text-overflow: ellipsis;
                vertical-align: middle;
                position: relative;
                text-align: left;
            }


            #map{
                width:400px;
                height:400px;
                margin-left: 5px
            }
            #editMap{
                width: 500px;
                height: 300px;
                margin:0;
                font-family:"微软雅黑";
            }
        </style>
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>预约管理<small>体检机构管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>预约管理</el-breadcrumb-item>
                    <el-breadcrumb-item>体检机构管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="名称/地址" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                        <el-button @click="handleCurrentChange(1)" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="体检机构名" align="center"></el-table-column>
                        <el-table-column prop="phoneNum" label="联系方式" align="center"></el-table-column>
                        <el-table-column prop="longitudeAndLatitude" label="经纬度" align="center"></el-table-column>
                        <el-table-column prop="addressImg" label="机构图片" align="center">
                            <template slot-scope="scope">
                                <img :src="scope.row.addressImg" alt="" width="100px" height="100px">
                            </template>
                        </el-table-column>
                        <el-table-column prop="address" label="详细地址" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增体检机构" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="体检机构名" prop="code">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="联系电话" prop="name">
                                            <el-input v-model="formData.phoneNum"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="上传图片">
                                            <el-upload
                                                    class="avatar-uploader"
                                                    action="/address/upload"
                                                    :auto-upload="autoUpload"
                                                    name="imgFile"
                                                    :show-file-list="false"
                                                    :on-success="handleAvatarSuccess"
                                                    :before-upload="beforeAvatarUpload">
                                                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                            </el-upload>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="经纬度" prop="code">
                                            <el-input v-model="formData.longitudeAndLatitude"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="18">
                                        <el-form-item label="详细地址" prop="code">
                                            <el-input v-model="formData.address"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="6">
                                        <el-button type="primary" @click="getGpsList()" round >设置定位</el-button>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑体检机构" :visible.sync="dialogFormVisible4Edit">
                            <el-form ref="dataEditForm" :model="editFormData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="体检机构名" prop="code">
                                            <el-input v-model="editFormData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="联系电话" prop="name">
                                            <el-input v-model="editFormData.phoneNum"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="上传图片">
                                            <el-upload
                                                    class="avatar-uploader"
                                                    action="/address/upload"
                                                    :auto-upload="autoUpload"
                                                    name="addressImg"
                                                    :show-file-list="false"
                                                    :on-success="handleAvatarSuccess"
                                                    :before-upload="beforeAvatarUpload">
                                                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                            </el-upload>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="经纬度" prop="code">
                                            <el-input v-model="editFormData.longitudeAndLatitude"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="18">
                                        <el-form-item label="详细地址" prop="code">
                                            <el-input v-model="editFormData.address"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <div id="editMap"></div>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

<!--                    地图弹框-->
                        <el-dialog title="体检机构" :visible.sync="dialogFormVisible4Map">
                            <div id="map" ></div>
                         <!--   <div id="search">
                                请输入:<input type="text" id="suggestId" size="20" value=""style="width:150px;"/>
                            </div>
                            <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>-->
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Map = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>


                </div>
            </div>
        </div>
    </body>

    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关属性
                    currentPage: 1,
                    pageSize:5,
                    total:100,
                    queryString:null,
                },
                dataList: [],//列表数据
                formData: {},//表单数据
                autoUpload:true,//自动上传
                imageUrl:null,//模型数据，用于上传图片完成后图片预览
                editFormData: {},//编辑表单数据
                dialogFormVisible: false,//控制添加窗口显示/隐藏
                dialogFormVisible4Edit:false,//控制编辑窗口显示/隐藏
                dialogFormVisible4Map:false//控制地图窗口显示/隐藏
            },
            created() {
                this.findPage();
                this.createMaps();
            },
            methods: {
                //分页查询
                findPage(){
                    var param = {
                        currentPage:this.pagination.currentPage,
                        pageSize:this.pagination.pageSize,
                        queryString:this.pagination.queryString
                    };
                    axios.post("/address/findPage",param).then((res) => {
                        this.pagination.total = res.data.total;
                        this.dataList = res.data.rows;
                        for (var p in this.dataList) {
                            this.dataList[p].addressImg= "http://qhu4540az.hd-bkt.clouddn.com/"+this.dataList[p].addressImg;
                        }
                    });

                },
                //切换页码
                handleCurrentChange(currentPage) {
                    this.pagination.currentPage = currentPage;
                    this.findPage();
                },
                //重置表单数据
                resetform(){
                    this.formData = {};
                    this.imageUrl = null;
                },
                //弹出新增框
                handleCreate(){
                    this.resetform();
                    this.dialogFormVisible = true;
                },
                //弹出编辑框
                handleUpdate(row){
                    this.dialogFormVisible4Edit = true;
                    this.$nextTick(() => {
                        setTimeout(() => {
                            this.createEditMap()
                        },1000)
                    });
                    axios.get("/address/findById?id="+row.id).then((res) => {
                            if (res.data.flag){
                                //查询成功
                                this.editFormData = res.data.data;
                                //显示图片
                                this.imageUrl = "http://qhu4540az.hd-bkt.clouddn.com/"+this.editFormData.addressImg;
                            }else {
                                //查询失败
                                this.$message.error(res.data.message)
                            }
                    });

                },
                //编辑数据
                handleEdit(){

                    axios.post("/address/edit",this.editFormData).then((res) => {
                        if(res.data.flag){
                            //修改成功
                            this.$message.success(res.data.message);
                        }else{
                            //修改失败
                            this.$messge.error(res.data.message);
                        }
                    }).finally(() => {
                        this.findPage();
                        this.dialogFormVisible4Edit = false;
                    })
                },
                //发送ajax请求,将体检机构信息传到后台
                handleAdd(){
                    //关闭弹框
                    this.dialogFormVisible = false;
                    axios.post("/address/add",this.formData).then((res) => {
                        if (res.data.flag){
                            //添加成功
                            this.$message({
                                type:"success",
                                message:res.data.message
                            });
                        } else{
                            //添加失败
                            this.$messsage.error(res.data.message);
                        }
                    }).finally(() => {
                        this.findPage();
                    })
                },
                //检查图片文件格式
                beforeAvatarUpload(file){
                    const isJPG = file.type === 'image/jpeg';
                    const isLt2M = file.size / 1024 / 1024 < 2;
                    if (!isJPG) {
                        this.$message.error('上传套餐图片只能是 JPG 格式!');
                    }
                    if (!isLt2M) {
                        this.$message.error('上传套餐图片大小不能超过 2MB!');
                    }
                    return isJPG && isLt2M;
                },

                //上传成功后的回调函数
                handleAvatarSuccess(response, file){
                    //该方法是上传组件提供的,这里的response相当于以前写的res.data,最后一个data是自己封装的,也就是将保存到数据库中的文件名
                    this.imageUrl="http://qhu4540az.hd-bkt.clouddn.com/"+response.data;
                    //给出提示信息
                    this.$message({
                       type:response.flag?'success':'error',
                       message:response.message
                    });
                    //将图片名保存到表单中,一起传到后台
                    this.formData.addressImg = response.data;
                },
                //删除
                handleDelete(row){
                    this.$confirm('此操作将永久删除该数据, 是否继续?', '提示',{
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then((res) => {
                        axios.get("/address/delete?id="+row.id).then((res) => {
                            if (res.data.flag){
                                this.$message.success(res.data.message);
                            } else{
                                this.$message.error(res.data.message);
                            }
                        }).finally(() =>{
                            this.findPage();
                        })
                    }).catch(() => {
                        this.$message({
                            type:'info',
                            message:'已取消删除'
                        });
                    });
                },
                getGpsList(){
                    this.dialogFormVisible4Map = true;
                    this.createMaps();
                   //this.searchMap();
                },
                //通过指定的毫秒数调用方法,否则第一次加载不出来
                createMaps(){
                    setTimeout(
                        () => {
                        this.createMap()
                    },100);
                },
                //创建地图实例
                createMap() {
                    // 创建Map实例
                    var map = new BMap.Map("map");
                    var geolocation = new BMap.Geolocation();
                    //调用百度地图api 中的获取当前位置接口
                    geolocation.getCurrentPosition(function (r) {
                        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                            //获取当前位置经纬度
                            let myGeo = new BMap.Geocoder();
                            //获得经度和纬度
                            map.addEventListener('click', function (e) {
                                myGeo.getLocation(e.point,function(rs){
                                    addComp = rs.addressComponents;
                                    vue.dialogFormVisible4Map = false;
                                    vue.formData.longitudeAndLatitude = (rs.point.lng + ',' + rs.point.lat);
                                    vue.formData.address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                                });
                            });
                            myGeo.getLocation(new BMap.Point(r.point.lng, r.point.lat), function (result) {
                                if (result) {
                                    console.log(result);
                                    // 初始化地图,设置中心点坐标和地图级别
                                    map.centerAndZoom(new BMap.Point(result.point.lng, result.point.lat), 11);//设置中心点
                                    map.setCurrentCity("合肥");        //由于有3D图，需要设置城市哦
                                    //开启鼠标滚轮缩放,默认关闭
                                    map.enableScrollWheelZoom(false)
                                    //添加缩略图控件
                                    map.addControl(new BMap.OverviewMapControl({
                                        isOpen: false,
                                        anchor: BMAP_ANCHOR_BOTTOM_RIGHT
                                    }));
                                    //添加缩放平移控件
                                    map.addControl(new BMap.NavigationControl());
                                    //添加比例尺控件
                                    map.addControl(new BMap.ScaleControl());
                                    //添加地图类型控件
                                    map.addControl(new BMap.MapTypeControl());
                                    var marker = new BMap.Marker(new BMap.Point(result.point.lng, result.point.lat));
                                    //把标注添加到地图上
                                    map.addOverlay(marker);
                                }
                            });
                        }
                    });
                },
                /*searchMap(){
                    // 百度地图API功能
                    function G(id) {
                        return document.getElementById(id);
                    }

                    var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
                        {"input" : "suggestId"
                            ,"location" : map
                        });

                    ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
                        var str = "";
                        var _value = e.fromitem.value;
                        var value = "";
                        if (e.fromitem.index > -1) {
                            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                        }
                        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                        value = "";
                        if (e.toitem.index > -1) {
                            _value = e.toitem.value;
                            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                        }
                        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                        G("searchResultPanel").innerHTML = str;
                    });

                    var myValue;
                    ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
                        var _value = e.item.value;
                        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                        G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

                        setPlace();
                    });
                    function setPlace(){
                        map.clearOverlays();    //清除地图上所有覆盖物
                        function myFun(){
                            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                            map.centerAndZoom(pp, 18);
                            map.addOverlay(new BMap.Marker(pp));    //添加标注
                        }
                        var local = new BMap.LocalSearch(map, { //智能搜索
                            onSearchComplete: myFun
                        });
                        local.search(myValue);
                    }
                },*/
                createEditMap(){
                    // 创建Map实例
                    var map = new BMap.Map("editMap");
                    var geolocation = new BMap.Geolocation();
                    //调用百度地图api 中的获取当前位置接口
                    geolocation.getCurrentPosition(function (r) {
                        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                            //获取当前位置经纬度
                            let myGeo = new BMap.Geocoder();
                            //获得经度和纬度
                            map.addEventListener('click', function (e) {
                                myGeo.getLocation(e.point,function(rs){
                                    addComp = rs.addressComponents;
                                    vue.editFormData.longitudeAndLatitude = (rs.point.lng + ',' + rs.point.lat);
                                    vue.editFormData.address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                                });
                            });
                            myGeo.getLocation(new BMap.Point(r.point.lng, r.point.lat), function (result) {
                                if (result) {
                                    console.log(result);
                                    var lal = vue.editFormData.longitudeAndLatitude;
                                    var arr = lal.split(",");
                                    var longitude = arr[0];
                                    var latitude = arr[1];
                                    // 初始化地图,设置中心点坐标和地图级别
                                    map.centerAndZoom(new BMap.Point(longitude, latitude), 11);//设置中心点
                                    map.setCurrentCity("合肥");        //由于有3D图，需要设置城市哦
                                    //开启鼠标滚轮缩放,默认关闭
                                    map.enableScrollWheelZoom(false)
                                    //添加缩略图控件
                                    map.addControl(new BMap.OverviewMapControl({
                                        isOpen: false,
                                        anchor: BMAP_ANCHOR_BOTTOM_RIGHT
                                    }));
                                    //添加缩放平移控件
                                    map.addControl(new BMap.NavigationControl());
                                    //添加比例尺控件
                                    map.addControl(new BMap.ScaleControl());
                                    //添加地图类型控件
                                    map.addControl(new BMap.MapTypeControl());

                                    var marker = new BMap.Marker(new BMap.Point(result.point.lng, result.point.lat));
                                    //把标注添加到地图上
                                    map.addOverlay(marker);
                                }
                            });
                        }
                    });
                }
            }
        });

    </script>
</html>
